package com.yupi.project.util;

import org.springframework.util.DigestUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * @author <a href=""https://github.com/Fzs0>逢左使</a>
 * @create 2024/01/06 11:43
 * @Description: 用户生成邮箱对应的账号和密码
 */
public class EmailUtil {

    /**
     * 盐值，混淆密码
     */
    private static final String SALT = "HP";

    private static final String defaultPassword = "12345678";

    public static String generateDefaultEncryptPassword() {
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT + defaultPassword).getBytes());
        return encryptPassword;
    }

    // 生成随机账号（8位长度）
    public static String generateRandomAccount() {
        return generateRandomString(8);
    }

    // 生成指定长度的随机字符串
    private static String generateRandomString(int length) {
        String CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder stringBuilder = new StringBuilder();
        SecureRandom secureRandom = new SecureRandom();

        for (int i = 0; i < length; i++) {
            int index = secureRandom.nextInt(CHARACTERS.length());
            stringBuilder.append(CHARACTERS.charAt(index));
        }

        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        String randomAccount = generateRandomAccount();
        System.out.println("Random Account: " + randomAccount);
        System.out.println("默认密码 " + generateDefaultEncryptPassword());
    }

}
